'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "";
            $scope.imgData={};
            $scope.param = {};
            if($state.includes('**.banner.update')){
                title="编辑用户";
                var id = $state.params.id;
                detail(id);
                $scope.isDisabled = false;
            }else if($state.includes('**.banner.create')){
                title="添加用户";
                $scope.record = {'bannerType' :1};
                $scope.isDisabled = false;
                $scope.flagShowOrHide=1;//用来控制排序号和是否可用是否显示
            }
            else if($state.includes('**.banner.postList')){
                title="我的帖子的列表页面";
                var mobile = $state.params.mobile;
                postList(mobile);
                $scope.isDisabled = false;
                $scope.flagShowOrHide=1;//用来控制排序号和是否可用是否显示
            }

            $scope.title = $rootScope.title = title;
            $scope.loading = true;

            //初始化验证
            $scope.submit= function(){
                $scope.loading = true;

                if(!$('#mobile').val()){
                    $scope.alertModal({'content':'手机号不能为空'});
                    return;
                }


                if(!$('#status').val()){
                    $scope.alertModal({'content':'状态不能为空'});
                    return;
                }

                saveData();
            };

            function saveData(){
                var m = $scope.record;
                console.log(m);
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.record.id ? $scope.app.project_name+'/userCenter/user/update' : $scope.app.project_name+'/platform/banner/add',
                        data: m
                    }).then(callback);
                }
                function callback(result){
                    if(result.httpCode ==200){
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            $state.go('main.uc.banner.list');
                        },2000);
                    }else{
                        $scope.alertModal({"content":"操作失败"});
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;
                }
            }

            // 初始化页面
            function detail(id) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/userCenter/user/detail',
                    data: {'id': id}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                        $scope.imgData = [];
                        var srcArr = [];
                        srcArr.push($scope.record.imgUrl);
                        $scope.imgData.push({
                            src:srcArr
                        })
                        console.log($scope.imgData)
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

           // 查询我的帖子的列表
            function postList(mobile,pageNum) {
                console.log($scope.param);
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/userCenter/user/myPost',
                    data: {'mobile': mobile,'pageNum':pageNum}
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();

                });
                $scope.mobile=mobile;
            }
           // 查询标签表的标签
            $scope.addLabel = function(id){
                $.ajax({
                    url : $scope.app.project_name + '/userCenter/user/labelList',
                    data: {'id': id}
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.$apply(function () {
                            $scope.pageInfos = result.data;
                        })
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();

                });
                $scope.id=id;
                $scope.firstApply = true;
            }

            // 查询添加过的帖子标签
            $scope.selectLabel = function(id){
                $.ajax({
                    url : $scope.app.project_name + '/userCenter/user/postLabelAlready',
                    data: {'id': id}
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.$apply(function () {
                            $scope.pageInfoAdd = result.data;
                        })
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();

                });
                $scope.id=id;
                $scope.firstApply1 = true;
            }


            // 全选操作
            $scope.all = function (m) {
                for(var i=0;i<$scope.pageInfos.length;i++){
                    if(m===true){
                        $scope.pageInfos[i].state=true;
                    }else {
                        $scope.pageInfos[i].state=false;
                    }
                }
            };
            // 获取复选框选中的值
            $scope.saveMakeUp = function (id) {

                var batchUpdates = new Array();//声明一个数组来保存多个的id值
                $(".checkitem").each(function () {
                    if(this.checked){
                        console.log($(this).val());
                        batchUpdates.push($(this).val());
                    }
                })
                 // 给帖子打标签
                $.ajax({
                    url : $scope.app.project_name + '/userCenter/user/addPostlabel',
                    data: {'id':id,'ids': batchUpdates.join(",")}
                }).then(function(result) {
                    console.log(result);
                    if(result.httpCode ==200){
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            postList($scope.mobile);
                            $scope.firstApply = false;
                        },2000);
                    }else{
                        $scope.alertModal({"content":"操作失败"});
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;

                });

            };
            // 翻页
            $scope.pagination = function (pageNo) {

                var mobile = $state.params.mobile;
                postList(mobile,pageNo);
            };

            // 帖子内容鼠标移入的动作
            $scope.content = [];
            $scope.preview = function(content){
                var showDiv = document.getElementById('showDiv');
                showDiv.style.left = event.clientX;
                showDiv.style.top = event.clientY;
                showDiv.style.display = 'block';
                showDiv.innerHTML = content;
            }
            // 帖子内容鼠标移出的动作
            $scope.outHide = function(){
                var showDiv = document.getElementById('showDiv');
                showDiv.style.display = 'none';
                showDiv.innerHTML = '';
            }

            // 帖子标题鼠标移入的动作
            $scope.title = [];
            $scope.preview1 = function(title){
                var showDiv1 = document.getElementById('showDiv1');
                showDiv1.style.left = event.clientX;
                showDiv1.style.top = event.clientY;
                showDiv1.style.display = 'block';
                showDiv1.innerHTML = title;
            }
            // 帖子标题鼠标移出的动作
            $scope.outHide1 = function(){
                var showDiv1 = document.getElementById('showDiv1');
                showDiv1.style.display = 'none';
                showDiv1.innerHTML = '';
            }

        }]);